package com.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.hutool.dfa.SensitiveUtil;
import com.entity.UserEntity;
import com.entity.YonghuEntity;
import com.service.YonghuService;
import com.service.impl.MailService;
import com.utils.ValidatorUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.annotation.IgnoreAuth;
import com.entity.ZhengshuxinxiEntity;
import com.entity.view.ZhengshuxinxiView;
import com.service.ZhengshuxinxiService;
import com.service.TokenService;
import com.utils.PageUtils;
import com.utils.R;
import com.utils.MPUtil;
import com.utils.CommonUtil;

/**
 * 证书信息Controller
 *后端接口
 */
@RestController
@RequestMapping("/zhengshuxinxi")
public class ZhengshuxinxiController {
    @Autowired
    private ZhengshuxinxiService zhengshuxinxiService;
    @Resource
    private YonghuService yonghuService;


    @Autowired
    private MailService mailService;
    
    
            /**
         * 后端列表
         */
        @RequestMapping("/page")
        public R page(@RequestParam Map<String, Object> params, ZhengshuxinxiEntity zhengshuxinxi, HttpServletRequest request) {
                            if (!request.getSession().getAttribute("role").toString().equals("管理员")) {
                    zhengshuxinxi.setUserid((Long) request.getSession().getAttribute("userId"));
                }
                                    EntityWrapper<ZhengshuxinxiEntity> ew = new EntityWrapper<ZhengshuxinxiEntity>();
            PageUtils page = zhengshuxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, zhengshuxinxi), params), params));
            request.setAttribute("data", page);
            return R.ok().put("data", page);
        }

        /**
         * 前端列表
         */
                @RequestMapping("/list")
        public R list(@RequestParam Map<String, Object> params, ZhengshuxinxiEntity zhengshuxinxi, HttpServletRequest request) {


                                    EntityWrapper<ZhengshuxinxiEntity> ew = new EntityWrapper<ZhengshuxinxiEntity>();
            PageUtils page = zhengshuxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, zhengshuxinxi), params), params));
            request.setAttribute("data", page);
            return R.ok().put("data", page);
        }

        /**
             * 列表
             */
        @RequestMapping("/lists")
        public R list( ZhengshuxinxiEntity zhengshuxinxi) {
            EntityWrapper<ZhengshuxinxiEntity> ew = new EntityWrapper<ZhengshuxinxiEntity>();
            ew.allEq(MPUtil.allEQMapPre( zhengshuxinxi, "zhengshuxinxi"));
            return R.ok().put("data", zhengshuxinxiService.selectListView(ew));
        }

        /**
         * 查询
         */
        @RequestMapping("/query")
        public R query(ZhengshuxinxiEntity zhengshuxinxi) {
            EntityWrapper< ZhengshuxinxiEntity> ew = new EntityWrapper< ZhengshuxinxiEntity>();
            ew.allEq(MPUtil.allEQMapPre( zhengshuxinxi, "Zhengshuxinxi"));
                ZhengshuxinxiView zhengshuxinxiView = zhengshuxinxiService.selectView(ew);
            return R.ok("证书信息").put("data", zhengshuxinxiView);
        }

        /**
             * 后端详情
             */
                    @RequestMapping("/info/{id}")
        public R info(@PathVariable("id") Long id) {
                ZhengshuxinxiEntity zhengshuxinxi = zhengshuxinxiService.selectById(id);
            return R.ok().put("data", zhengshuxinxi);
        }

        /**
            * 前端详情
            */
                    @RequestMapping("/detail/{id}")
        public R detail(@PathVariable("id") Long id) {
                ZhengshuxinxiEntity zhengshuxinxi = zhengshuxinxiService.selectById(id);
                        return R.ok().put("data", zhengshuxinxi);
        }

        /**
        * 后端保存
        */
        @RequestMapping("/save")
        public R save(@RequestBody ZhengshuxinxiEntity zhengshuxinxi, HttpServletRequest request) {

            // 校验敏感词

            boolean b = SensitiveUtil.containsSensitive(zhengshuxinxi.getZhengshumingcheng());
            boolean c = SensitiveUtil.containsSensitive(zhengshuxinxi.getJianglici());
            if (b || c) {
                return R.error("包含敏感词");
            }
            zhengshuxinxi.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
            //ValidatorUtils.validateEntity(address);
                            zhengshuxinxi.setUserid((Long) request.getSession().getAttribute("userId"));
                            zhengshuxinxiService.insert(zhengshuxinxi);
            return R.ok();
        }

        /**
            * 前端保存
            */
        @RequestMapping("/add")
        public R add(@RequestBody ZhengshuxinxiEntity zhengshuxinxi, HttpServletRequest request) {
                    
            zhengshuxinxi.setUserid((Long) request.getSession().getAttribute("userId"));

                        zhengshuxinxi.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
            //ValidatorUtils.validateEntity(yiyuanjianjie);

                zhengshuxinxiService.insert(zhengshuxinxi);
            return R.ok();
        }

        /**
          * 修改
          */
        @RequestMapping("/update")
        public R update(@RequestBody ZhengshuxinxiEntity zhengshuxinxi, HttpServletRequest request) {
            //ValidatorUtils.validateEntity(zaixianbaoming);
            ZhengshuxinxiEntity zhengshuxinxiEntity = zhengshuxinxiService.selectById(zhengshuxinxi.getId());
            if (zhengshuxinxiEntity.getType().equals("不通过")){
                if (zhengshuxinxi.getType().equals("通过")){
                    YonghuEntity yonghuEntity = yonghuService.selectById(zhengshuxinxi.getUserid());

                    try {
                        mailService.sendSimpleMail(yonghuEntity.getLianxifangshi(), "证书状态", "证书已审核通过");
                    } catch (Exception e) {
                        e.printStackTrace();

                    }
                }
            }
                zhengshuxinxiService.updateById(zhengshuxinxi);//全部更新
            return R.ok();
        }

        /**
             * 删除
             */
        @RequestMapping("/delete")
        public R delete(@RequestBody Long[] ids) {
                zhengshuxinxiService.deleteBatchIds(Arrays.asList(ids));
            return R.ok();
        }
    

    
    
}
